/*
 * Copyright @ 2024 weili23
 * com.itheima.mp.controller 10:11
 * All right reserved.
 */

package com.itheima.mp.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itheima.mp.service.IFileCommonService;
import com.itheima.mp.utils.StoreFileReturn;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 文件通用
 * @author: weili23
 * @createTime: 2024/5/14 10:11
 * @version: v1.0
 */
@RestController
@RequestMapping("/file")
@Api(tags ="文件通用接口")
public class FileCommonController {
    @Autowired
    private IFileCommonService fileCommonService;

    @ApiOperation("单文件上传")
    @PostMapping(value="/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public StoreFileReturn fileUpload(@RequestPart("file")  MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return fileCommonService.uploadFile(file);
    }

    @ApiOperation("多文件上传")
    @PostMapping(value="/uploadBatch",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public List<StoreFileReturn> uploadBatch(@RequestPart("file") List<MultipartFile> files) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        Assert.isTrue(!files.isEmpty(), "附件内容不能为空");
        List<StoreFileReturn> storePathExtList = new ArrayList<>();
        for (MultipartFile file : files) {
            StoreFileReturn storePathExt = fileCommonService.uploadFile(file);
            storePathExtList.add(storePathExt);
        }
        return storePathExtList;
    }

    @ApiOperation("文件下载")
    @PostMapping("/download")
    public void download(String path, @RequestParam(required = false)  String fileName, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(fileName)) {
            fileName = path;
        }
        fileCommonService.downloadFile(path,fileName,response);
    }

    @ApiOperation("文件下载")
    @GetMapping("/getdownload")
    public void getdownload(String path, @RequestParam(required = false)  String fileName, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(fileName)) {
            fileName = path;
        }
        fileCommonService.downloadFile(path,fileName,response);
    }

    @ApiOperation("删除文件")
    @PostMapping("/delete")
    public void delete(@RequestParam(required = true)String path) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        fileCommonService.deleteFile(path);
    }
}
